package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.domain.SysLog;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.domain.vo.QuestiontypeQueryCriteria;
import me.zhengjie.modules.system.domain.vo.TeachtypeQueryCriteria;
import me.zhengjie.modules.system.service.*;
import me.zhengjie.service.SysLogService;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Security;
import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static me.zhengjie.utils.TreeDataUtil.*;

@RestController
@RequiredArgsConstructor
@Api(tags = "学校教材管理")
@RequestMapping("/api/screenData")
public class ScreenDataController {

    private final SysLogService sysLogService;

    private final UserService userService;

    private final StudentService studentService;

    private final TeacherService teacherService;

    private final TeachService teachService;

    private final TeachtypeService teachtypeService;

    private final QuestiontypeService questiontypeService;

    private final QuestionService questionService;

    private final  ResourcetypeService resourcetypeService;

    private final TeskAndstudentService teskAndstudentService;

    private final ResourceService resourceService;

    private final ChapterlearnrecordService chapterlearnrecordService;

    private final TeskService teskService;

    private final UserexercisesetlogService userexercisesetlogService;

    //获取教师数量 学生数量 教材数量 资源数量 今日访问数量 今日学习数量 今日任务数量
    @GetMapping("/getCountObj")
    @Log("查询数量统计信息")
    @ApiOperation("查询数量统计信息")
    public ResponseEntity<Map<String,Object>> getCountObj(){
        Map<String,Object> map = new HashMap<>();
        User user = userService.getById(SecurityUtils.getCurrentUserId());

        //学生数量
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enabled",1);
        queryWrapper.eq("schoolId",user.getDeptId());
        Long studentCount = studentService.count(queryWrapper);
        map.put("studentCount",studentCount);

        //教师数量
        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("enabled",1);
        queryWrapper1.eq("schoolId",user.getDeptId());
        Long teacherCount = teacherService.count(queryWrapper1);
        map.put("teacherCount",teacherCount);

        //教材数量
        Long teachCount = teachService.queryTeachCount(Math.toIntExact(user.getDeptId()));
        map.put("teachCount",teachCount);

        //资源数量
        QueryWrapper queryWrapper3 = new QueryWrapper<>();
//        queryWrapper3.eq("state",1);
        queryWrapper3.eq("schoolId",user.getDeptId());
        Long resourceCount =  resourceService.count(queryWrapper3);
        map.put("resourceCount",resourceCount);

        //今日访问数量
        // 获取当前本地时间
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 转换为Timestamp
        Timestamp startTimestamp = Timestamp.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Timestamp endTimestamp = Timestamp.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        QueryWrapper queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.lt("createTime",endTimestamp);
        queryWrapper4.ge("createTime",startTimestamp);
        queryWrapper4.isNotNull("userId");
        queryWrapper4.groupBy("userId");
//        Long sysLogCount =  sysLogService.count(queryWrapper4);
        List<SysLog> list = sysLogService.list(queryWrapper4);
        map.put("sysLogCount",list.size());

        //今日学习数量
        QueryWrapper queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.lt("startTime",endTimestamp);
        queryWrapper5.ge("startTime",startTimestamp);
        queryWrapper5.eq("schoolId",user.getDeptId());
        queryWrapper5.groupBy("userId");
//        Long chapterlearnrecordCount = chapterlearnrecordService.count(queryWrapper5);
        List<Chapterlearnrecord> list1 = chapterlearnrecordService.list(queryWrapper5);
        System.out.println("list1"+list1);
        map.put("chapterlearnrecordCount",list1.size());

        //今日任务数量
        QueryWrapper queryWrapper6 = new QueryWrapper<>();
        queryWrapper6.lt("createBy",endTimestamp);
        queryWrapper6.ge("createBy",startTimestamp);
        queryWrapper6.eq("schoolId",user.getDeptId());
        Long teskCount = teskService.count(queryWrapper6);
        map.put("teskCount",teskCount);

        return new ResponseEntity<>(map, HttpStatus.OK);
    }

    //获取今日活跃
    @GetMapping("/getUserActive")
    @Log("查询用户活跃")
    @ApiOperation("查询用户活跃")
    public ResponseEntity<List<Integer>> getUserActive(){

        //今日访问数量
        // 获取当前本地时间
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 转换为Timestamp
        Timestamp startTimestamp = Timestamp.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Timestamp endTimestamp = Timestamp.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        User user = userService.getById(SecurityUtils.getCurrentUserId());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("deptId",user.getDeptId());
//        queryWrapper.eq("userType",2);
        queryWrapper.lt("createTime",endTimestamp);
        queryWrapper.ge("createTime",startTimestamp);
//        queryWrapper.orderByDesc("createTime");
        queryWrapper.groupBy("userId");
        List<SysLog> sysLogList = sysLogService.list(queryWrapper);

        // 获取当前时间
        LocalTime now = LocalTime.now();

        // 获取今天午夜的时间
        LocalTime midnight = LocalTime.MIDNIGHT;

        // 计算从午夜到现在的时间差
        Duration durationSinceMidnight = Duration.between(midnight, now);

        // 转换为小时数
        Long hoursSinceMidnight = durationSinceMidnight.toHours();

        List<Integer> list = new ArrayList<>();
        for(Integer item = 0; item<Integer.valueOf(Math.toIntExact(hoursSinceMidnight)); item++){
            Integer count = 0;
            for(SysLog sysLog: sysLogList){

                LocalDateTime localDateTime = sysLog.getCreateTime().toLocalDateTime();

                Integer hourFromLocalDateTime = localDateTime.getHour();
//                System.out.println("hourFromLocalDateTime"+hourFromLocalDateTime);
//                System.out.println("item"+item);
//                System.out.println(item == hourFromLocalDateTime);
                if(hourFromLocalDateTime.equals(item + 1)){
                    count = count+1;
                }
            }

            list.add(count);
        }

        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    //获取题库分类数量
    @GetMapping("/getQuestionClassifyNum")
    @Log("获取题库分类数量")
    @ApiOperation("获取题库分类数量")
    public ResponseEntity<List<Map<String,Object>>> getQuestionClassifyNum(){

        User user = userService.getById(SecurityUtils.getCurrentUserId());
        QuestiontypeQueryCriteria questiontypeQueryCriteria = new QuestiontypeQueryCriteria();
//        questiontypeQueryCriteria.setSchoolId(user.getDeptId());


        List<Questiontype> list1 = questiontypeService.queryAll(questiontypeQueryCriteria);


        List<Integer> questionTypeIds = new ArrayList<>();
        for(Questiontype questiontype:list1){
            questionTypeIds.add(questiontype.getId());
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("questionTypeId",questionTypeIds);
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Question> questionList = new ArrayList<>();
        if(questionTypeIds.size()!=0){
            questionList = questionService.list(queryWrapper);
        }

        for(Questiontype questiontype: list1){
            Integer num = 0;
            for(Question question:questionList){
                if(Objects.equals(questiontype.getId(), question.getQuestionTypeId())){
                    num = num +1;
                }
            }
            questiontype.setNum(num);
            Float currentProportion = (float) num/questionList.size();
            questiontype.setProportion(currentProportion * 100);
        }

        List<Map<String,Object>> list3 = new ArrayList<>();
        for(Questiontype queryType :list1){
            Map<String, Object> map = convertObjectToMap(queryType);
            map.put("id",queryType.getId());
            map.put("childList",new ArrayList<>());
            list3.add(map);
        }

        List<Map<String,Object>> list4 = list3.stream().filter(m -> m.get("parentId") == null).peek(
                (m) -> m.put("childList", getChildren(m,list3))
        ).collect(Collectors.toList());
        List<Map<String,Object>> mapList = getResultList(list4);

        return new ResponseEntity<>(mapList, HttpStatus.OK);
    }

    //获取教材分类数量
    @GetMapping("/getTeachClassifyNum")
    @Log("获取教材分类数量")
    @ApiOperation("获取教材分类数量")
    public ResponseEntity<List<Map<String,Object>>> getTeachClassifyNum(){

        User user = userService.getById(SecurityUtils.getCurrentUserId());
        TeachtypeQueryCriteria teachtypeQueryCriteria = new TeachtypeQueryCriteria();
//        teachtypeQueryCriteria.setSchoolId(user.getDeptId());
        QueryWrapper queryWrapper7 = new QueryWrapper<>();
        queryWrapper7.eq("dept_id",user.getDeptId());
        List<User> userList = userService.list(queryWrapper7);
        List<Long> userIds = new ArrayList<>();
        for(User user1:userList){
            userIds.add(user1.getId());
        }
        List<Teachtype> list1 = teachtypeService.queryAll(teachtypeQueryCriteria);


        List<Integer> teachTypeIds = new ArrayList<>();
        for(Teachtype teachtype:list1){
            teachTypeIds.add(Math.toIntExact(teachtype.getId()));
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teachTypeId",teachTypeIds);
        queryWrapper.in("userId",userIds);
        List<Teach> teachList = new ArrayList<>();
        if(teachTypeIds.size()!=0){
            teachList = teachService.list(queryWrapper);
        }
        System.out.println("==teachList=="+teachList);

        for(Teachtype teachtype: list1){
            Integer num = 0;
            for(Teach teach:teachList){
                if(Math.toIntExact(teachtype.getId()) == teach.getTeachtypeid()){
                    num = num +1;
                }
            }
            teachtype.setNum(num);
            Float currentProportion = (float) (Integer.valueOf(num)/teachList.size());

            teachtype.setProportion(currentProportion*100);
        }

        List<Map<String,Object>> list3 = new ArrayList<>();
        for(Teachtype teachtype :list1){
            Map<String, Object> map = convertObjectToMap(teachtype);
            map.put("id",teachtype.getId());
            map.put("childList",new ArrayList<>());
            list3.add(map);
        }

        List<Map<String,Object>> list4 = list3.stream().filter(m -> m.get("parentId") == null).peek(
                (m) -> m.put("childList", getChildren(m,list3))
        ).collect(Collectors.toList());
        List<Map<String,Object>> mapList = getResultList(list4);

        return new ResponseEntity<>(mapList, HttpStatus.OK);
    }

    //获取近30天活跃变化
    @GetMapping("/getActiveChange")
    @Log("获取近30天活跃变化")
    @ApiOperation("获取近30天活跃变化")
    public ResponseEntity<Map<String,Object>> getActiveChange(){
        //今日访问数量
        // 获取当前本地时间
        LocalDateTime startOfDay = LocalDateTime.now().minusDays(30).withHour(0).withMinute(0).withSecond(0).withNano(0);
//        System.out.println("startOfDay"+startOfDay);

        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 转换为Timestamp
        Timestamp startTimestamp = Timestamp.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Timestamp endTimestamp = Timestamp.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        User user = userService.getById(SecurityUtils.getCurrentUserId());

//        list 学习人数
        List<Integer> list = getPeopleNum(user,startTimestamp,endTimestamp);
        //完成测试
        List<Integer> list2 = getUserExerciseSetLogNum(user,startTimestamp,endTimestamp);
        //任务完成
        List<Integer> list3 =  getTeskCompleteNum(user,startTimestamp,endTimestamp);
        Map<String,Object> map = new HashMap<>();
        map.put("studyNum",list);
        map.put("testNum",list2);
        map.put("teskNum",list3);

        return new ResponseEntity<>(map, HttpStatus.OK);
    }
    //获取测试完成数
    public List<Integer> getUserExerciseSetLogNum(User user,Timestamp startTimestamp,Timestamp endTimestamp){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Student> studentList = studentService.list(queryWrapper);
        List<Long> userIds = new ArrayList<>();
        for(Student student:studentList){
            userIds.add(student.getUserid());
        }

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("isComplete",1);
        queryWrapper1.in("userId",userIds);
        queryWrapper1.lt("createTime",endTimestamp);
        queryWrapper1.ge("createTime",startTimestamp);
        List<Userexercisesetlog> userExerciseSetLogList = userexercisesetlogService.list(queryWrapper1);


        List<Timestamp> timestamps = generateDailyTimestamps(29);
        Collections.reverse(timestamps);


        List<Integer> list = new ArrayList<>();

        for(Timestamp timestamp:timestamps){
            Integer count = 0;

            for(Userexercisesetlog userExerciseSetLog:userExerciseSetLogList){

                if(Objects.equals(String.valueOf(timestamp), String.valueOf(userExerciseSetLog.getCreatetime()))){

                    count = count + 1;


                }
            }

            list.add(count);

        }
        return list;
    }
    //获取三十天任务完成
    public List<Integer> getTeskCompleteNum(User user,Timestamp startTimestamp,Timestamp endTimestamp){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Student> studentList = studentService.list(queryWrapper);
        List<Long> userIds = new ArrayList<>();
        for(Student student:studentList){
            userIds.add(student.getUserid());
        }

        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("state",1);
        queryWrapper1.in("userId",userIds);
        queryWrapper1.lt("lastUpdateTime",endTimestamp);
        queryWrapper1.ge("lastUpdateTime",startTimestamp);
        List<TeskAndstudent> teskAndstudentList = teskAndstudentService.list(queryWrapper1);


        List<Timestamp> timestamps = generateDailyTimestamps(29);
        Collections.reverse(timestamps);


        List<Integer> list = new ArrayList<>();

        for(Timestamp timestamp:timestamps){
            Integer count = 0;

            for(TeskAndstudent teskAndstudent:teskAndstudentList){

                if(Objects.equals(String.valueOf(timestamp), String.valueOf(teskAndstudent.getLastUpdateTime()))){

                    count = count + 1;


                }
            }

            list.add(count);

        }
        return list;
    }
    //获取三十天活跃人数
    public List<Integer> getPeopleNum(User user,Timestamp startTimestamp,Timestamp endTimestamp){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("schoolId",user.getDeptId());
//        queryWrapper.eq("userType",2);
        queryWrapper.lt("startTime",endTimestamp);
        queryWrapper.ge("startTime",startTimestamp);
        List<Chapterlearnrecord> chapterlearnrecordList = chapterlearnrecordService.list(queryWrapper);


        List<Chapterlearnrecord> chapterlearnrecords = new ArrayList<>();
        for(Chapterlearnrecord chapterlearnrecord:chapterlearnrecordList){
            // 将Timestamp转换为LocalDateTime
            LocalDateTime localDateTime = chapterlearnrecord.getStartTime().toLocalDateTime();

            // 使用DateTimeFormatter将LocalDateTime格式化为字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = localDateTime.format(formatter);
            // 截取前10个字符，即日期部分
            String datePart = formattedDateTime.substring(0, 10);
            Integer state = 0;
            for(Chapterlearnrecord chapterlearnrecord1:chapterlearnrecords){
                LocalDateTime localDateTime1 =  chapterlearnrecord1.getStartTime().toLocalDateTime();
                String formattedDateTime1 = localDateTime1.format(formatter);
                String datePart2 = formattedDateTime1.substring(0, 10);
                if(Objects.equals(datePart, datePart2) && Objects.equals(chapterlearnrecord.getUserId(), chapterlearnrecord1.getUserId())){
                    state = 1;
                }
            }
            if(state == 0){
                // 将Timestamp转换为ZonedDateTime
                ZonedDateTime zonedDateTime = chapterlearnrecord.getStartTime().toInstant().atZone(ZoneId.systemDefault());

                // 将ZonedDateTime转换为LocalDateTime
                LocalDateTime currentlocalDateTime = zonedDateTime.toLocalDateTime();

                // 将LocalDateTime的时间部分设置为0点
                LocalDateTime zeroTime = currentlocalDateTime.withHour(0).withMinute(0).withSecond(0).withNano(0);

                // 将LocalDateTime转换回ZonedDateTime，然后转换为Timestamp
                Timestamp currentTimestamp = Timestamp.from(zeroTime.atZone(ZoneId.systemDefault()).toInstant());
                chapterlearnrecord.setStartTime(currentTimestamp);
                chapterlearnrecords.add(chapterlearnrecord);
            }
        }


        List<Timestamp> timestamps = generateDailyTimestamps(29);
        Collections.reverse(timestamps);


        List<Integer> list = new ArrayList<>();

        for(Timestamp timestamp:timestamps){
            Integer count = 0;

            for(Chapterlearnrecord chapterlearnrecord:chapterlearnrecords){


                if(String.valueOf(timestamp).equals(String.valueOf(chapterlearnrecord.getStartTime()))){

                    count = count + 1;
                }
            }
            list.add(count);

        }
        return list;
    }

    private static List<Timestamp> generateDailyTimestamps(int daysBack) {
        List<Timestamp> timestamps = new ArrayList<>();
        LocalDate today = LocalDate.now();

        for (int i = 0; i <= daysBack; i++) {
            LocalDate date = today.minusDays(i);
            LocalDateTime startOfDay = LocalDateTime.of(date, LocalDateTime.MIN.toLocalTime());

            Timestamp timestamp = Timestamp.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
            timestamps.add(timestamp);
        }

        return timestamps;
    }

    //资源分类占比
    @GetMapping("/getResourcesRatio")
    @Log("资源分类占比")
    @ApiOperation("资源分类占比")
    public ResponseEntity<List<TypeDo>> getResourcesRatio(){

        User user = userService.getById(SecurityUtils.getCurrentUserId());


        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Resource> resourceList = resourceService.list(queryWrapper);

        List<TypeDo> typeDoList = new ArrayList<>();

        for(Integer i = 0;i<9;i++){
            TypeDo typeDo = new TypeDo();
            switch (i) {
                case 0:
                    typeDo.setName("图片");
                    break;
                case 1:
                    typeDo.setName("音频");
                    break;
                case 2:
                    typeDo.setName("视频");
                    break;
                case 3:
                    typeDo.setName("二维码动画");
                    break;
                case 4:
                    typeDo.setName("三维动画");
                    break;
                case 5:
                    typeDo.setName("虚拟交互模型");
                    break;
                case 6:
                    typeDo.setName("文档");
                    break;
                case 7:
                    typeDo.setName("压缩包");
                    break;
                default:
                    typeDo.setName("矢量图片");
            }
            Integer num = 0;
            for(Resource resource:resourceList){
                if(i.equals(resource.getType())){
                    num = num + 1;
                }
            }
            typeDo.setNum(num);
            typeDo.setProportion(((float) (num/resourceList.size()))*100);
            typeDoList.add(typeDo);
        }

        return new ResponseEntity<>(typeDoList, HttpStatus.OK);
    }

    //题库难度占比
    @GetMapping("/getQuestionRatio")
    @Log("题库难度占比")
    @ApiOperation("题库难度占比")
    public ResponseEntity<List<TypeDo>> getQuestionRatio(){
        User user = userService.getById(SecurityUtils.getCurrentUserId());

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Question> questionList = questionService.list(queryWrapper);

        List<TypeDo> typeDoList = new ArrayList<>();

        for(Integer i = 0;i<10;i++){
            TypeDo typeDo = new TypeDo();
            typeDo.setName("难度"+(i+1));
            Integer num = 0;
            for(Question question:questionList){
                if(i.equals(question.getDifficulty())){
                    num = num + 1;
                }
            }
            typeDo.setNum(num);
            typeDo.setProportion(((float) (num/questionList.size()))*100);
            typeDoList.add(typeDo);
        }

        return new ResponseEntity<>(typeDoList, HttpStatus.OK);
    }

    //获取任务变化
    @GetMapping("/getTeskActive")
    @Log("获取任务完成变化")
    @ApiOperation("获取任务完成变化")
    public ResponseEntity<List<Integer>> getTeskActive(){
        User user = userService.getById(SecurityUtils.getCurrentUserId());
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Student> userList = studentService.list(queryWrapper);
        List<Long> userIds = new ArrayList<>();
        for(Student user1:userList){
            userIds.add(user1.getUserid());
        }


        //今日访问数量
        // 获取当前本地时间
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);

        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);

        // 转换为Timestamp
        Timestamp startTimestamp = Timestamp.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Timestamp endTimestamp = Timestamp.from(endOfDay.atZone(ZoneId.systemDefault()).toInstant());

        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lt("lastUpdateTime",endTimestamp);
        queryWrapper1.ge("lastUpdateTime",startTimestamp);
        queryWrapper1.eq("state",1);
        queryWrapper1.in("userId",userIds);
        List<TeskAndstudent> teskAndstudentList = teskAndstudentService.list(queryWrapper1);

        // 获取当前时间
        LocalTime now = LocalTime.now();

        // 获取今天午夜的时间
        LocalTime midnight = LocalTime.MIDNIGHT;

        // 计算从午夜到现在的时间差
        Duration durationSinceMidnight = Duration.between(midnight, now);

        // 转换为小时数
        Long hoursSinceMidnight = durationSinceMidnight.toHours();

        List<Integer> list = new ArrayList<>();
        for(Integer item = 0; item<Integer.valueOf(Math.toIntExact(hoursSinceMidnight)); item++){
            Integer count = 0;
            for(TeskAndstudent teskAndstudent: teskAndstudentList){

                LocalDateTime localDateTime = teskAndstudent.getLastUpdateTime().toLocalDateTime();

                Integer hourFromLocalDateTime = localDateTime.getHour();

                if(hourFromLocalDateTime.equals(item + 1)){
                    count = count+1;
                }
            }
            list.add(count);
        }

        return new ResponseEntity<>(list, HttpStatus.OK);
    }

    //获取任务完成日志
    @GetMapping("/getTeskComplete")
    @Log("获取任务完成日志")
    @ApiOperation("获取任务完成日志")
    public ResponseEntity<List<TeskDo>> getTeskComplete(){
        User user = userService.getById(SecurityUtils.getCurrentUserId());



        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolId",user.getDeptId());
        List<Student> userList = studentService.list(queryWrapper);
        List<Long> userIds = new ArrayList<>();
        for(Student user1:userList){
            userIds.add(user1.getUserid());
        }

        QueryWrapper queryWrapper1 = new QueryWrapper<>();
//        queryWrapper1.lt("lastUpdateTime",endTimestamp);
//        queryWrapper1.ge("lastUpdateTime",startTimestamp);
        queryWrapper1.eq("state",1);
        queryWrapper1.in("userId",userIds);
        queryWrapper1.orderByDesc("lastUpdateTime");
        List<TeskAndstudent> teskAndstudentList = teskAndstudentService.list(queryWrapper1);

        //获取任务
        List<Integer> teskIds = new ArrayList<>();
        for(TeskAndstudent teskAndstudent:teskAndstudentList){
            teskIds.add(teskAndstudent.getTeskId());
        }

        List<Tesk> teskList = new ArrayList<>();
        if(teskIds.size()>0){
            teskList = teskService.listByIds(teskIds);
        }

        List<TeskDo> teskDoList = new ArrayList<>();
        for(TeskAndstudent teskAndstudent:teskAndstudentList){
            TeskDo teskDo = new TeskDo();
            teskDo.setIsComplete(teskAndstudent.getState());
            teskDo.setTime(String.valueOf(teskAndstudent.getLastUpdateTime()));
            for(Tesk tesk:teskList){
                if(Objects.equals(tesk.getId(), teskAndstudent.getTeskId())){
                    teskDo.setTeskName(tesk.getName());
                }
            }
            for(Student student:userList){
                if(Objects.equals(String.valueOf(student.getUserid()), String.valueOf(teskAndstudent.getUserId()))){
                    teskDo.setUserName(student.getName());
                }
            }
            teskDoList.add(teskDo);
        }


        return new ResponseEntity<>(teskDoList, HttpStatus.OK);
    }

}
